﻿//===============================================================================
// Microsoft Innovation Centre - Western Europe
// Copyright (c) 2008 Microsoft Corporation. All rights reserved.
//
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================

using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;

namespace Microsoft.WVB.Framework.AdControls
{
    /// <summary>
    /// An Ad layout control providing functionnality to display Ads (any format) overlayed on top
    /// of the Video display surface.
    /// </summary>
    [TemplatePart(Name = AdOverlay.BumperAreaName, Type = typeof(ContentControl)),
    TemplatePart(Name = AdOverlay.TopOverlayAreaName, Type = typeof(ContentControl)),
    TemplatePart(Name = AdOverlay.BottomOverlayAreaName, Type = typeof(ContentControl)),
    TemplatePart(Name = AdOverlay.PipAreaName, Type = typeof(ContentControl))]
    public sealed class AdOverlay : AdLayoutBase
    {
        private const string BumperAreaName = "BumperArea";
        private const string TopOverlayAreaName = "TopOverlayArea";
        private const string BottomOverlayAreaName = "BottomOverlayArea";
        private const string PipAreaName = "PipArea";
        
        internal ContentControl BumperArea = null;
        internal ContentControl TopOverlayArea = null;
        internal ContentControl BottomOverlayArea = null;
        internal ContentControl PipArea = null;

        /// <summary>
        /// The Active ContentControl hosting the Ad.
        /// </summary>
        internal ContentControl ActiveControl = null;
        
        /// <summary>
        /// The public constructor that initialize a new AdOverlay instance of that class.
        /// </summary>
        public AdOverlay()
            : base()
        {
            this.DefaultStyleKey = typeof(AdOverlay);
        }

        /// <summary>
        /// Builds the visual tree for the AdOverlay control when a new template is applied.
        /// Also, this is where internal logic hooks with UI elements part of the TemplatePart contract.
        /// </summary>
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            BumperArea = base.GetTemplateChild(AdOverlay.BumperAreaName) as ContentControl;
            TopOverlayArea = base.GetTemplateChild(AdOverlay.TopOverlayAreaName) as ContentControl;
            BottomOverlayArea = base.GetTemplateChild(AdOverlay.BottomOverlayAreaName) as ContentControl;
            PipArea = base.GetTemplateChild(AdOverlay.PipAreaName) as ContentControl;
        }
        /// <summary>
        /// When overriden, provides a palceholder for developer to control actions to execute 
        /// when the Ad needs to be shown on the screen.
        /// </summary>
        public override void OnBeginShowAd()
        {
        }

        public override void OnAdStopped()
        {
            this.OnEndShowAd();
        }

        /// <summary>
        /// Provides a placeholder for executing actions that an AdOverlay needs to execute when an Ad
        /// ends.
        /// </summary>
        public override void OnEndShowAd()
        {
            if (this.ActiveControl != null)
            {
                this.ActiveControl.Content = null;
            }
        }

        /// <summary>
        /// Provides a placeholder for executing actions that an AdOverlay needs to execute when an Ad
        /// is being buffered (downloaded from a resource or Ad server).
        /// </summary>
        public override void OnBufferingAd(FrameworkElement Ad)
        {
            if (this.Context.DisplayRegion == AdOverlayMode.BottomOverlayed)
            {
                this.ActiveControl = BottomOverlayArea;
                this.BottomOverlayArea.Content = Ad;
            }
            else if (this.Context.DisplayRegion == AdOverlayMode.Inline)
            {
                this.ActiveControl = BumperArea;
                this.BumperArea.Content = Ad;
            }
            else if (this.Context.DisplayRegion == AdOverlayMode.Pip)
            {
                this.ActiveControl = PipArea;
                this.PipArea.Content = Ad;
            }
            else if (this.Context.DisplayRegion == AdOverlayMode.TopOverlayed)
            {
                this.ActiveControl = TopOverlayArea;
                this.TopOverlayArea.Content = Ad;
            }
            Ad.UpdateLayout();
        }
    }
}
